package php.code.instructions;

import com.google.common.base.Joiner;
import utils.titles.Title;
import php.code.Variable;

public class VariableDeclaration extends Instruction {
    private final Variable variable;
    private int pad;
    private String value;

    public VariableDeclaration(Title title, int pad, String value) {
        this(new Variable(title), pad, value);
    }

    public VariableDeclaration(Variable variable, int pad, String value) {
        super(false);
        this.variable = variable;
        this.pad = pad;
        this.value = value;
    }

    public VariableDeclaration(Title title) {
        this(new Variable(title), 0, null);
    }

    public VariableDeclaration(Title title, String value) {
        this(new Variable(title), 0, value);
    }

    public VariableDeclaration(String name, String value) {
        this(new Variable(name), 0, value);
    }

    public VariableDeclaration(Variable variable, String value) {
        this(variable, 0, value);
    }

    public VariableDeclaration(String value) {
        this(new Variable(value), 0, null);
    }

    public Variable getVariable() {
        return variable;
    }

    @Override
    protected String getCode() {
        String pattern = pad > 0 ? String.format("%%-%ds", pad) : "%s";
        return Joiner
                .on("")
                .skipNulls()
                .join(
                        getIndentation(),
                        String.format(pattern, getVariable()),
                        value == null ? null : " = " + value
                );
    }
}
